﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;

namespace BlueLotus7.Common
{
    public class SerializeUtility
    {
        BinaryFormatter formatter = new BinaryFormatter();

        public SerializeUtility() { }

        public MemoryStream SerializeToStream(object objectToSerialize)
        {
            MemoryStream memoryStream = new MemoryStream();
            try
            {
                formatter.Serialize(memoryStream, objectToSerialize);
                return memoryStream;
            }
            finally
            {
                memoryStream.Close();
            }
        }
        public byte[] SerializeToByteArray(object objectToSerialize)
        {
            return SerializeToStream(objectToSerialize).ToArray();
        }
        public string SerializeToBase64String(object objectToSerialize)
        {
            byte[] byteArray = SerializeToByteArray(objectToSerialize);
            return Convert.ToBase64String(byteArray, 0, byteArray.Length);
        }
        public string SerializeToXml<T>(T objectToSerialize, Encoding encoding, bool indentedXml)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType());
                stream = new MemoryStream();
                writer = new XmlTextWriter(stream, encoding);
                if (indentedXml)
                {
                    writer.Formatting = Formatting.Indented;
                }

                serializer.Serialize(writer, objectToSerialize);
                return encoding.GetString(stream.ToArray());
            }
            finally
            {
                if (stream != null) stream.Close();
                if (writer != null) writer.Close();
            }
        }
        public string SerializeToXml<T>(T objectToSerialize)
        {
            return SerializeToXml<T>(objectToSerialize, Encoding.UTF8, false);
        }

        public object DerializeFromStream(MemoryStream stream)
        {
            try
            {
                object result = formatter.Deserialize(stream);
                return result;
            }
            finally
            {
                stream.Close();
            }
        }
        public object DerializeFromByteArray(byte[] byteArray)
        {
            MemoryStream memoryStream = new MemoryStream(byteArray, 0, byteArray.Length);
            return DerializeFromStream(memoryStream);
        }
        public object DerializeFromBase64String(string base64String)
        {
            byte[] byteArray = Convert.FromBase64String(base64String);
            return DerializeFromByteArray(byteArray);
        }
        public T DerializeFromXml<T>(string xml, Encoding encoding)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StreamReader memoryStream = new StreamReader(new MemoryStream(encoding.GetBytes(xml)));

            try
            {
                return (T)serializer.Deserialize(memoryStream);
            }
            finally
            {
                memoryStream.Close();
            }
        }
        public T DerializeFromXml<T>(string xml)
        {
            return DerializeFromXml<T>(xml, Encoding.UTF8);
        }
    }
}
